/**
 * 
 */
package com.res.system.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;

import com.res.system.properties.PropertySelector;

/**
 * @author da40188
 * 
 */
public class SimulatorUtilities {

	public static Map<String, String> convertPropertyIntoMap(final Properties properties) {

		final Map<String, String> map = new LinkedHashMap<String, String>((Map) properties);
		return map;
	}

	public static Map<String, String> sort(final Map<String, String> map) {

		final Map<String, String> sortedMap = new LinkedHashMap<String, String>();
		if (null != map) {
			sortedMap.putAll(map);
		}

		return sortedMap;
	}

	/**
	 * 
	 * @param apiMap
	 * @return
	 */
	public static List<String> getSortedList(Map<String, String> apiMap) {

		SortedSet<String> catagorySet = new TreeSet<String>();
		for (String value : apiMap.keySet()) {

			catagorySet.add(value);
		}
		List<String> catagorylist = new ArrayList<String>(catagorySet);
		return catagorylist;
	}

	public static String getConfigData(final ConfigData configData) {

		String configDataValue = null;
		Map<String, String> configurationDataMap = getConfigurationData();

		if (configData.getConfigDataValue().equals(ConfigData.JOBTYPE.getConfigDataValue())) {
			configDataValue = configurationDataMap.get(ConfigData.JOBTYPE.getConfigDataValue());
		} else if (configData.getConfigDataValue().equals(ConfigData.SERVER_ADDRESS.getConfigDataValue())) {
			configDataValue = configurationDataMap.get(ConfigData.SERVER_ADDRESS.getConfigDataValue());
		} else if (configData.getConfigDataValue().equals(ConfigData.SERVER_PORT.getConfigDataValue())) {
			configDataValue = configurationDataMap.get(ConfigData.SERVER_PORT.getConfigDataValue());
		}

		return configDataValue;

	}

	/**
	 * 
	 * @param executorKey
	 * @return
	 */
	public static String getExecutorName(final String executorKeyValue) {
		String executorName = null;
		final Map<String, String> executorMap = PropertySelector.getInstance().select("executorMappingProperties");

		for (String executorKey : executorMap.keySet()) {

			String key = executorKey.toLowerCase();

			if (key.equals(executorKeyValue)) {
				System.out.println("Executor name========>" + executorKey);
				executorName = executorMap.get(executorKey);
				break;
			}

		}

		return executorName;
	}

	/**
     * 
     */
	private static Map<String, String> getConfigurationData() {

		final Map<String, String> configurationDataMap = new HashMap<String, String>();

		final Map<String, String> simulatorPropertyMap = PropertySelector.getInstance().select("simulatorProperties");
		final Map<String, String> systemConfigurationPropertyMap = PropertySelector.getInstance().select("configProperties");

		configurationDataMap.put(ConfigData.JOBTYPE.getConfigDataValue(), simulatorPropertyMap.get("catalogManagement"));
		configurationDataMap.put(ConfigData.SERVER_ADDRESS.getConfigDataValue(), systemConfigurationPropertyMap.get("internalserveraddress"));
		configurationDataMap.put(ConfigData.SERVER_PORT.getConfigDataValue(), systemConfigurationPropertyMap.get("internalserverport"));

		return configurationDataMap;

	}

	/*
     *  
     */
	public enum ConfigData {
		JOBTYPE("jobType"), SERVER_ADDRESS("address"), SERVER_PORT("port");

		private String configData = null;

		// VefiryAT type Enum constructor
		ConfigData(String configData) {

			this.configData = configData;
		}

		public String getConfigDataValue() {

			return configData;
		}
	}
}
